package servlet;

import impl.UserDaoImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.server.UID;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import pojo.User;

/**
 * Servlet implementation class ShowAllServlet
 */
public class ShowAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowAllServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");                      //解决中文乱码
		response.setHeader("content-type","text/html;charset=UTF-8");//解决中文乱码
		PrintWriter out = response.getWriter();
		UserDao userDao = new UserDaoImpl();
		List<User> ul = userDao.loadAllUsers();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		List<User> ul =new ArrayList<User>();
//		User u =new User("haha", 16.5, new Date(), false);
//		ul.add(u);
		out.println("<html>");
		out.println("<head><title>用户信息</title></head>");
		out.println("<body>");
		if(ul==null){
			out.println("暂无用户信息</br>");
		}
		else{
			for(User uu:ul){
				out.println("姓名:"+uu.getUname()+"__工资:"+uu.getSalary()+"__生日:"+sdf.format(uu.getBirth())+"__性别:"+uu.isSex()+"</br>");
			}
		}
		out.println("</body>");
		out.println("</html>");
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
